<?php


namespace app\merchant\system\service;


use app\common\model\SysConfig;

class MerConfigService
{
    static function getConfig($tenant_id, $key)
    {
        $data = SysConfig::where("key", '=', $key)
            ->where("tenant_id", '=', $tenant_id)
            ->find();
        return json_decode($data['value'], true) ?? [];
    }

    static function getData($tenant_id, $key)
    {
        $data = SysConfig::where("key", '=', $key)
            ->where("tenant_id", '=', $tenant_id)
            ->find();
        $data['value'] = json_decode($data['value'], true) ?? [];
        return $data;
    }

    static function setConfig($tenant_id, $key, $value)
    {
        $ex = SysConfig::where("key", '=', $key)
                ->where("tenant_id", '=', $tenant_id)
                ->count() > 0;
        if ($ex) {
            SysConfig::where("key", '=', $key)
                ->where("tenant_id", '=', $tenant_id)
                ->update([
                    'value' => json_encode($value),
                ]);
        } else {
            SysConfig::insert([
                'key' => $key,
                'value' => json_encode($value),
            ]);
        }

    }
}